package com.itheima.web.exceptions;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CustomExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        e.printStackTrace();
        ModelAndView model = new ModelAndView();
        if (e instanceof AuthenticationException || e instanceof UnauthorizedException) {
            model.setViewName("forward:/unauthorized.jsp");
        } else {
            model.setViewName("error");
        }
        model.addObject("errorMsg", "系统忙，请稍后再试...");
        return model;
    }
}
